// pages/pintuan/pintuan.js
var regexp = require('../../utils/regexpUtil.js');
var cart = require('../../utils/cart.js');
var util = require('../../utils/util.js');
var pintuanapi = require('../../component/service/pintuanapi.js');
var countDownT = require('../../component/countDown/countDownT.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skuId: '',
    pintuanId:'',
    pintuan:{},
    hidden: false,
    sku: {},
    desc:'',
    showSection:'desc',
    quantity: 1,
    images:[],
    comment:{
      total:{
        commentVal:100,
        commentVal1:100,
        commentVal2:100,
        commentVal3:100,
        commentVal4:100,
        commentVal5:100,
      }
    },//评价详情
    sales:[],//最近成交列表
    totalPrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      pintuanId: options.itemId
    }),
    pintuanapi.item(this.data.pintuanId,function(res){
      let countDown = new countDownT();
      countDown.setExpireTime(res.item.serverTime,res.item.expireTime,"pintuan.countDownT",that,res.item.validTime);
      res.item.countDownT = countDown;
      res.item.percent = parseInt(res.item.actualQuantity*100/res.item.planQuantity);
      that.setData({
        pintuan:res.item,
        skuId:res.sku.skuId,
        hidden:true,
        quantity:res.item.minQuantity
      });
      that.getSku();
      that.getSkuDesc();
      that.skuImages();
      that.effectPrice();
    })
  },
  getSku:function(){
    var that = this;
    wx.request({
    url: getApp().globalData.host + '/webapp/resource/getSku.html?skuId=' + this.data.skuId,
    header: getApp().globalData.header,
      success: function (res) {
        if(typeof(res.data)!='undefined'&&res.data.skuId!=''){
          that.setData({
            sku: res.data,
            hidden:true
          });
        }
      }
    })
  },
  getSkuDesc:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.host + '/webapp/resource/skuDesc.html?skuId=' + this.data.skuId,
      header: getApp().globalData.header,
      success: function (res) {
        that.setData({
          desc: getApp().globalData.imgHost + regexp.regexpImg(res.data.desc)
        });    
      }
    })
  },
  skuImages:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.host + '/webapp/resource/skuImages.html?skuId=' + that.data.skuId,
      header: getApp().globalData.header,
      success: function (res) {
        if(typeof(res.data)!='undefined'&&res.data.length>0){
          that.setData({
            images:res.data
          });
        }
      }
    })
  },
  previewImg:function(e){
    var imgUrl = e.currentTarget.dataset.imgUrl;
    if (typeof (imgUrl) != 'undefined' && imgUrl.length>0) {
      let urls = [];
      urls.push(imgUrl);
      wx.previewImage({
        urls: urls
      })
    }
  },
  changeSection:function(e){
    this.setData({
      showSection:e.target.dataset.name
    })
  },
  reduce: function(){
    if(this.data.quantity<=this.data.pintuan.minQuantity) return;
    this.modifyQuantity(-1);
  },
  add: function(){
    this.modifyQuantity(1);
  },
  modifyQuantity:function(num){
    var quantity = this.data.quantity+num;
    if(quantity>this.data.sku.actualWholesaleMaxQuantity) quantity=this.data.sku.actualWholesaleMaxQuantity;
    this.setData({
      quantity: quantity
    });
    this.effectPrice();
  },
  quantityChange:function(e){
    var reg = /^[1-9]+[0-9]*]*$/; //判断正整数
    var changetQty = e.detail.value;
    if(reg.test(changetQty)){
      changetQty = parseInt(changetQty);
      if(changetQty<this.data.pintuan.minQuantity) changetQty=this.data.pintuan.minQuantity;
      if(changetQty>this.data.sku.actualWholesaleMaxQuantity) changetQty=this.data.sku.actualWholesaleMaxQuantity;
      this.setData({
        quantity: changetQty
      });
      this.effectPrice();
    }else{
      this.setData({
        quantity: this.data.quantity
      });
    }
  },
  //设置有效价格 后台列表按minQuantity升序，所以最后一个符合要求的价格为生效价格
  effectPrice:function(){
    this.data.totalPrice = this.data.pintuan.price*this.data.quantity;
    this.setData({
      totalPrice:this.data.totalPrice
    });
  },
  buyNow:function(e){
    var that = this;
    if (getApp().globalData.platUser == null) {//用户未登录 跳转至登录页面
      wx.navigateTo({
        url: '../login/login'
      });
    }else{
      pintuanapi.buyNow(this.data.pintuanId,this.data.quantity,function(res){
        wx.navigateTo({
          url: '../confirm/confirm?orderId=' + res
        });
      });
    }
  },
  //分享该页面
  onShareAppMessage: function () {
    return {
      title: '[分享] ' + this.data.pintuan.skuName,
      path: '/pages/pintuan/pintuan?itemId=' + this.data.itemId
    }
  },
  //返回首页
  goHome:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //跳转购物清单
  shoppingcart:function(){
    wx.switchTab({
      url: '/pages/shoppingCart/shoppingCart'
    })
  },
  //拨打客服电话
  contactService:function(){
    wx.makePhoneCall({
      phoneNumber: getApp().globalData.servicePhone
    })
  }
})